package com.wxxymaker.controller;

import com.wxxymaker.interceptor.annocation.AuthRequire;
import com.wxxymaker.model.entity.Shop;
import com.wxxymaker.model.entity.StageCount;
import com.wxxymaker.service.ShopService;
import com.wxxymaker.service.StageCountService;
import com.wxxymaker.utils.Const;
import com.wxxymaker.utils.web.ResponseWarp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Soft
 * @Date: 2018/5/8 21:39
 * @Desc: 公司控制器
 */
@Controller
@RequestMapping("/shop")
public class ShopHandler {
    @Autowired
    private ShopService shopService;

    @Autowired
    private StageCountService stageCountService;

    /**
     * 刷新所有公司总分统计
     */
    @PostMapping("/refresh")
    @ResponseBody
    public ResponseWarp refresh() {
        List<Shop> all = shopService.findAll();
        for (Shop shop : all) {
            double v = stageCountService.countTotalForShop(shop);
            shopService.setTotalAndPrev(shop, shop.getPrevTotal(), v);
        }
        return ResponseWarp.ok();
    }

    /**
     * 添加公司
     */
    @PostMapping("/add")
    @ResponseBody
    public ResponseWarp add(Shop shop) {
        Shop add = shopService.add(shop);
        if (null != add) {
            return ResponseWarp.ok(add, "添加成功！");
        }
        return ResponseWarp.error("添加失败！");
    }

    /**
     * 根据id删除公司
     * @param id 公司id
     */
    @PostMapping("/delete/{id}")
    @ResponseBody
    public ResponseWarp delete(@PathVariable Integer id) {
        Shop shop = shopService.delete(id);
        if (null != shop) {
            return ResponseWarp.ok();
        }
        return ResponseWarp.error("删除失败！");
    }

    /**
     * 公司信息修改
     * @param shop 带有id的公司的信息
     */
    @PostMapping("/change")
    @ResponseBody
    public ResponseWarp change(Shop shop) {
        if (shopService.change(shop)) {
            return ResponseWarp.ok("修改成功！");
        }
        return ResponseWarp.error("修改失败！");
    }

    /**
     * 根据id获取公司信息
     * @param id 公司id
     */
    @AuthRequire
    @PostMapping("/byid/{id}")
    @ResponseBody
    public ResponseWarp get(@PathVariable Integer id) {
        Shop one = shopService.getOne(id);
        if (null != one) {
            return ResponseWarp.ok(one);
        }
        return ResponseWarp.error("获取失败！");
    }

    /**
     * 分页查询
     * @param pageNo 页码
     * @param type   公司类型（可选）
     */
    @AuthRequire
    @PostMapping("/page")
    @ResponseBody
    public ResponseWarp page(@RequestParam(defaultValue = "1") Integer pageNo,
                             @RequestParam(defaultValue = "0") Integer type) {
        Page<Shop> page;
        if (type > 0)
            page = shopService.page( pageNo, Const.pageSize, type);
        else
            page = shopService.page( pageNo, Const.pageSize);
        if (page != null && page.getContent().size() > 0) {
            return ResponseWarp.ok(page);
        }
        return ResponseWarp.error("没有分页数据！");
    }

    /**
     * 根据公司名查询，模糊查询
     * @param name 公司名
     */
    @AuthRequire
    @PostMapping("/byname/{name}")
    @ResponseBody
    public ResponseWarp name(@PathVariable String name) {
        Shop one = shopService.getByName(name);
        if (null != one) {
            return ResponseWarp.ok(one);
        }
        return ResponseWarp.error("获取失败！");
    }

    /**
     * 添加公司界面
     */
    @GetMapping("/add")
    public String add_(ModelMap map) {
        map.put("shopTypeMap", Const.shopTypeMap);
        return "/admin/shops-ac";
    }

    /**
     * 修改公司信息
     * @param id 公司id
     */
    @GetMapping("/change/{id}")
    public String change_(ModelMap map, @PathVariable Integer id) {
        map.put("shopTypeMap", Const.shopTypeMap);
        map.put("shop", shopService.getOne(id));
        return "/admin/shops-ac";
    }
}
